package com.bainuo.doctor.common.base;

import android.app.Activity;
import android.graphics.Bitmap;

import com.bainuo.doctor.common.util.ViewUtil;
import com.blankj.utilcode.utils.Utils;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.internal.Supplier;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

import org.litepal.LitePalApplication;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 百诺医生app Aplication基础类
 * Created by MonkeyShen on 2017/1/17.
 * Mail:shenminjie92@sina.com
 */
public class BaseApplication extends LitePalApplication {


    /**
     * 保存
     */
    private Map<String, Activity> mActivityCache;

//    private Typeface typeface;
    private static BaseApplication instance;
    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);
        this.instance = this;
//        typeface = Typeface.createFromAsset(getAssets(),"fonts/Lantinghei_0.ttf");

        mActivityCache = new HashMap<>();
        ViewUtil.init(this);

        initFresco();
    }

    /**
     * 初始化Fresco
     */

//    public Typeface getTypeface() {
//        return typeface;
//    }

    public static BaseApplication getInstance() {
        return instance;
    }

//    public void setTypeface(Typeface typeface) {
//        this.typeface = typeface;
//    }

    /**
     * 保存activity
     *
     * @param activity activity
     */
    public void saveActivity(Activity activity) {
        mActivityCache.put(activity.getClass().getName(), activity);
    }

    /**
     * 清除保存的活动
     *
     * @param activity activity
     */
    public void removeActivity(Activity activity) {
        if (mActivityCache.containsKey(activity.getClass().getName())) {
            mActivityCache.remove(activity.getClass().getName());
        }
    }

    /**
     * 获取
     *
     * @param activityName 活动activity 全路劲
     * @return
     */
    public Activity getActivity(String activityName) {
        return mActivityCache.get(activityName);
    }

    /**
     * 初始化Fresco
     */
    private void initFresco() {
        DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder(this)
                .setMaxCacheSize(200 * 1024 * 1024)//最大缓存 --200M
                .setBaseDirectoryPathSupplier(new Supplier<File>() {
                    @Override
                    public File get() {
                        return getCacheDir();
                    }
                })
                .build();

        ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
                .setMainDiskCacheConfig(diskCacheConfig)
                .setDownsampleEnabled(true)
                .setBitmapsConfig(Bitmap.Config.RGB_565)
                .build();
        Fresco.initialize(this, config);
    }
}
